<?php
namespace app\app\controller;

use think\Controller;
class Logincode extends Controller {
    
    public function index() {
        if(request()->isPost()) {
            //获取相关的数据
            $data = input('post.');
            $username = $data['loginname'];
            $expireTime = time() + 600;
            $ret = model('AppLoginError')->get(['username'=>$username]);
            if($ret){
                $count = $ret->pwd_err;
                $curTime = time();
                if ($ret->expire_time > $curTime) {
                    $appUserData = [
                        'pwd_err' => $count + 1,
                        'expire_time' => $expireTime,
                    ];
                    model('AppLoginError')->updateById($appUserData, $ret->username);
                }
                else {
                    $appUserData = [
                        'pwd_err' => 1,
                        'expire_time' => $expireTime,
                    ];
                    model('AppLoginError')->updateById($appUserData, $ret->username);
                }
                
            }
            else{
                $appUserData = [
                    'username' => $username,
                    'pwd_err' => 1,
                    'expire_time' => $expireTime,
                ];
                model('AppLoginError')->add($appUserData);
            }
            
            $retCount = model('AppLoginError')->get(['username'=>$username]);
            
            $getData = $retCount->pwd_err;
            return json_encode($getData,JSON_UNESCAPED_UNICODE);
        }
    }
    
    public function losefocus() {
        if(request()->isPost()) {
            //获取相关的数据
            $data = input('post.');
            $username = $data['loginname'];
            $ret = model('AppLoginError')->get(['username'=>$username]);
            if($ret){
                $count = $ret->pwd_err;
            }
            else{
                $count = 0;
            }
            return json_encode($count,JSON_UNESCAPED_UNICODE);
        }
    }
}
